from django.db.models import F
from django.shortcuts import render,redirect
from rest_framework.views import APIView
from rest_framework.views import Response
import  redis
from myapp.models import *
# Create your views here.
r= redis.Redis('localhost',6379)
class Login(APIView):
    def get(self,request):
        username = request.GET.get('username')
        password = request.GET.get('password')
        print(username,password)
        user_obj=User.objects.filter(username=username,password=password).first()
        if user_obj:
            # 存入session  代表以登录
            r.set(user_obj.username,1)
            return Response({'code': 200,'msg':'登录成功','uid':user_obj.id})
        else:
            return Response({'code': 403,'msg':'登录失败'})

from myapp.serializers import *
class Add_News(APIView):
    def get(self,request):
        # 使用 F对象判断大于
         # __gt 大于   F(包裹参数)
        news= News.objects.filter(total_views__gt=F('total_comment')).all()
        news_ser= News_ser(news,many=True)
        return Response({'code':200,'data':news_ser.data})

    def post(self,request):
        data={
            'title': request.POST.get('title'),
            'content':request.POST.get('content'),
            'user_id':request.POST.get('user_id'),
            'total_comment':request.POST.get('total_comment',0),
            'total_views':request.POST.get('total_views',0),
        }
        print(data)
        new_Ser= News_ser(data=data)
        print(new_Ser.is_valid())

        if new_Ser.is_valid():
            new_Ser.save()
        return Response({'code': 200, 'msg': '添加成功'})

from django.utils.decorators import method_decorator
def is_login(func):
    def inner(request):
        uid=request.GET.get('uid')
        user= User.objects.get(pk = uid)

        if r.get(user.username):
            print('登录成功')
        else:
            # return redirect('http://localhost:8080/login')
            return Response({'code':403})
        return func(request)
    return inner

class add_comment(APIView):
    @method_decorator(is_login)
    def get(self,request):
        uid = request.GET.get('uid')
        return Response({'code': 200, 'msg': 'ok'})

    def post(self,request):
        news_id=request.POST.get('news_id')
        print(news_id)
        comment = Comment_ser(data=request.data)
        print(comment.is_valid())
        if comment.is_valid():
            comment.save()
            #  对总数量加1
            news = News.objects.get(pk=news_id)
            count  = news.total_comment
            news.total_comment=int(count)+1
            news.save()
        return Response({'code': 200, 'msg': '评论成功'})

class Get_news(APIView):
    def get(self,request):
        id=request.GET.get('id')

        news = News.objects.get(pk= id)
        ip = request.META.get('REMOTE_ADDR')
        # 如果获取不到ip
        if not r.get(ip):
            # 记录ip
            r.set(ip,1)
            r.expire(ip,60)
            # 浏览次数+1
            news.total_views= int(news.total_views)+1
            news.save()
        news_Ser = News_ser(news)
        commnets=Comment.objects.filter(news_id=id).all()
        comment_Ser = Comment_ser(commnets,many=True)
        return Response({'code':200,'news':news_Ser.data,'comments':comment_Ser.data})